#include <typedefs.h>
#include <blower.h>
#include <lowLevelPwm.h>

/// <summary>
/// Initialize LPC1227 PWM
/// </summary>
void lowLevelPwm_init(void)
{
	// go ahead and enable clock for CT32B1
	LPC_SYSCON->SYSAHBCLKCTRL |= (1 << PWM_TIMER_CLOCK_CONTROL_INDEX);

	// Set PIO0_7 as MAT32B1 - Valid for LPCXpresso board
	LPC_IOCON->PIO0_7 &= ~0x00000007;
	LPC_IOCON->PIO0_7 |= 0x00000004;

	PWM_TIMER->TCR = PWM_TCR_RESET;      // reset timer
	PWM_TIMER->PR  = SystemCoreClock/10; // prescale to 100 millisecond
	PWM_TIMER->IR  = 0xFF;               // clear interrupts
	//PWM_TIMER->MR1 = 10;                 // Set MAT channel 1 match time
	//PWM_TIMER->MR3 = 20;                 // Set MAT channel 3 match time
	PWM_TIMER->MCR = 0;
	PWM_MCR_SET_MATCH_N(PWM_MR3, PWM_MCR_MATCH_RESET);  // reset on match channel 3, per NXP UM
	PWM_ENABLE_OUTPUT_N(PWM_MAT_ONE);    // Enable output
	//PWM_TIMER->TCR = 0x01;               // start timer
}
